'************************************************************************************
' Copyright © 2003-2005 Kathleen Dollard
'
' This software is provided 'as-is', without any express or implied warranty. In no 
' event will the author be held liable for any damages arising from the use of this
' software.
' 
' You may use this software for any purpose, including commercial applications, 
' and you may redistribute this software as part of application delivery: however, 
' this copyright notice may not be removed and you may not otherwise redistribute 
' this software directly other than providing a link to wwww.gendotnet.com. 
'
' You may alter this software to better server your needs (I'd like to hear about your 
' changes in order to make the software better). If you alter the softwre, you must  
' clearly mark it as altered and must not be misrepresented as the original software.
' nor wholly your original work.
'*************************************************************************************
Option Strict On
Option Explicit On 

Imports System

Namespace KADGen.CodeGenerationSupport
   Public Class XSLTProcessFile
      Inherits MultiPass

#Region "Class level declarations"
      Private mXsltTransform As Xml.Xsl.XslCompiledTransform
      Private mExtObjects() As CodeGenerationSupport.ExtObject
      Private mInputFileName As String
      Private mXsltFileName As String
      Private mStatusDirective As IStatusDirective
      Private mXsltProcessFileDirective As CodeGenerationSupport.IXSLTProcessFileDirective
      Private mMultiPassDirective As CodeGenerationSupport.IMultiPassDirective
      Dim mNsMgr As Xml.XmlNamespaceManager
#End Region

#Region "Public properties and methods"
      Public Sub Run( _
                     ByVal node As Xml.XmlNode, _
                     ByVal runUtility As RunUtility, _
                     ByVal statusDirective As IStatusDirective)

         mStatusDirective = statusDirective
         mMultiPassDirective = CType(node, CodeGenerationSupport.IMultiPassDirective)
         mXsltProcessFileDirective = CType(node, CodeGenerationSupport.IXSLTProcessFileDirective)
         mNsMgr = Utility.xmlHelpers.BuildNamespacesManagerForRoot(node.OwnerDocument)
         Try
            OpenXsltFile(mXsltProcessFileDirective, statusDirective)
            Me.MultiPass(mMultiPassDirective, _
                     Nothing, Nothing, _
                     AddressOf XSLTGenerationCallBack, _
                     statusDirective, _
                     runUtility)
         Catch ex As System.Exception
            'Diagnostics.Debug.WriteLine("")
            ' Exception should already be recorded
         End Try
      End Sub
#End Region

#Region "Protected properties and methods"
      Protected Function SetupParams( _
               ByVal filename As String, _
               ByVal xsltParams As CodeGenerationSupport.XSLTParam()) _
               As CodeGenerationSupport.XSLTParam()
         Return KADGen.CodeGenerationSupport.XSLTSupport.GetXSLTParamsFromStylesheet( _
                  filename, "", _
                  xsltParams)
      End Function
#End Region

#Region "Private properties and methods"
      Private Function OpenXmlFile(ByVal fileName As String) _
               As Xml.XmlDocument
         Dim statusOutput As CodeGenerationSupport.IStatusOutput
         Dim xmlDoc As New Xml.XmlDocument
         Const source As String = "XsltGeneration.OpenXmlFile"
         Try
            xmlDoc.Load(fileName)
         Catch ex As System.Exception
            statusOutput = mStatusDirective.AddStatusOutput( _
                     "<none>", _
                     FileTreatment.Unknown)
            statusOutput.AddStatusFailure(source, _
                     "Failure opening input metadata file", ex)
            Throw
         End Try
         Try
            mExtObjects = mXsltProcessFileDirective.XSLTFile.ExtObjects
         Catch ex As System.Exception
            statusOutput = mStatusDirective.AddStatusOutput( _
                     "<none>", _
                     FileTreatment.Unknown)
            statusOutput.AddStatusFailure(source, _
                     "Failure creating extension objects", ex)
            Throw
         End Try
         Return xmlDoc
      End Function

      Private Sub OpenXsltFile( _
               ByVal xsltGen As CodeGenerationSupport.IXSLTProcessFileDirective, _
               ByVal statusDirective As CodeGenerationSupport.IStatusDirective)
         Const source As String = "XsltGeneration.OpenXsltFile"
         Dim statusOutput As CodeGenerationSupport.IStatusOutput
         If (mXsltTransform Is Nothing Or mXsltFileName Is Nothing) _
                 OrElse mXsltFileName <> xsltGen.XSLTFile.XSLTFileName Then
            Try
               mXsltTransform = New Xml.Xsl.XslCompiledTransform
               mXsltTransform.Load(xsltGen.XSLTFile.XSLTFileName)
            Catch ex As System.Exception
               statusOutput = statusDirective.AddStatusOutput( _
                        "<none>", _
                        FileTreatment.Unknown)
               statusOutput.AddStatusFailure(source, _
                        "Failure loading XSLT (Filename=" & _
                              xsltGen.XSLTFile.XSLTFileName, ex)
               Throw
            End Try
            Try
               SetParams(SetupParams( _
                        xsltGen.XSLTFile.XSLTFileName, _
                        xsltGen.XSLTFile.XSLTParams))
            Catch ex As System.Exception
               statusOutput = statusDirective.AddStatusOutput( _
                        "<none>", _
                        FileTreatment.Unknown)
               statusOutput.AddStatusFailure(source, _
                        "Failure creating parameters", ex)
               Throw
            End Try
         End If
      End Sub

      Private Function XSLTGenerationCallBack( _
               ByVal outputFileName As String, _
               ByVal multiPassDirective As CodeGenerationSupport.IMultiPassDirective, _
               ByVal nodeSelect As Xml.XmlNode, _
               ByVal runUtility As CodeGenerationSupport.RunUtility, _
               ByVal statusOutput As CodeGenerationSupport.IStatusOutput) _
               As IO.Stream
         Dim xmlMetadata As Xml.XmlDocument
         Dim fileName As String

         fileName = IO.Path.Combine( _
                  CodeGenerationSupport.RunUtility.ParseFilenamePattern(mXsltProcessFileDirective.ProcessFile.InputDir, nodeSelect, mNsMgr), _
                  CodeGenerationSupport.RunUtility.ParseFilenamePattern(mXsltProcessFileDirective.ProcessFile.InputFilePattern, nodeSelect, mNsMgr))
         xmlMetadata = Me.OpenXmlFile(fileName)
         Me.SetParameters(nodeSelect, outputFileName)
         Return CodeGenerationSupport.XSLTSupport.GenerateViaXSLT( _
                              mXsltTransform, xmlMetadata, _
                              multiPassDirective.OutputRule.OutputFileType.ToString, _
                              mExtObjects, Params)
      End Function

#End Region
   End Class
End Namespace